package Lono::Parser::Ingredient;

use strict;
use warnings;

use Parse::RecDescent;
use Data::Dumper;

# TODO: look into using Regexp::Assemble...

sub new {
    my $class = shift;
    my $self = {};

    # Create a parser
    my $p = Parse::RecDescent->new(_grammar());
    $self->{'_p'} = $p;

    bless $self, $class;
    return $self;
}

sub parse {
    my $self = shift;
    my $raw = shift;

    #$::RD_HINT++;
    #$::RD_TRACE++;
    my $p = Parse::RecDescent->new(_grammar());
    
    my $output = $p->Ingredient($raw);
    #print Dumper($output);
    
    return $output;
}

sub _grammar {
    my $text = q[
Ingredient: Amount(?) OtherStuff
    {
        {'amount' => ${$item[1]}[0]};
    }

Amount: RealAmount 
      | SymbolicAmount
        
RealAmount: MixedNumber 
          | Decimal
          | Fraction
          | WholeNumber 

MixedNumber: WholeNumber Fraction
    {
        $return = $item[1] . ' ' . $item[2];
    }

WholeNumber: /\\d+/

Fraction: m!\\d+/\\d+!

Decimal: /(\\d+)?\\.\\d+/

SymbolicAmount: /(Pinch)/i

OtherStuff: /.*?/

];

    return $text;
}

1;
